package cn.itfei.lottery.domain.rule.service.engine.impl;

import cn.itfei.lottery.domain.rule.model.aggregates.TreeRuleRich;
import cn.itfei.lottery.domain.rule.model.req.DecisionMatterReq;
import cn.itfei.lottery.domain.rule.model.res.EngineResult;
import cn.itfei.lottery.domain.rule.model.vo.TreeNodeVO;
import cn.itfei.lottery.domain.rule.repository.IRuleRepository;
import cn.itfei.lottery.domain.rule.service.engine.EngineBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:    规则引擎处理器
 * @author：hfb
 * @date: 2023/11/16
 */
@Service("ruleEngineHandle")
public class RuleEngineHandle extends EngineBase {


    @Resource
    private IRuleRepository ruleRepository;


    @Override
    public EngineResult process(DecisionMatterReq matter) {
        // 决策规则树
        TreeRuleRich treeRuleRich = ruleRepository.queryTreeRuleRich(matter.getTreeId());
        if (null == treeRuleRich) {
            throw new RuntimeException(" Tree Rule is null");
        }

        // 决策节点
        TreeNodeVO treeNodeInfo = engineDecisionMaker(treeRuleRich, matter);// 找到叶子节点信息（首先从根开始，应该走左还是右，再从孩子比如说根走的左孩子，再从左孩子走：走左还是右，直到走到叶子节点，叶子节点就是当前用户经过age、性别过滤后能参与的活动）

        // 决策结果
        return new EngineResult(matter.getUserId(), treeNodeInfo.getTreeId(), treeNodeInfo.getTreeNodeId(), treeNodeInfo.getNodeValue());
    }

}
